
#include <BMaterial>
#include <BRender>
#include <BCube>
#include "member_BCube.h"

using namespace BWE;

#define member					(*(member_BCube*)_ptr)
#define member_allocate()		_ptr = new member_BCube(this)
#define member_release()		delete (member_BCube*)_ptr

BCube::BCube()
{
	member_allocate();
}
BCube::BCube(float length, float width, float height, int lengthSegments, int widthSegments, int heightSegments)
{
	member_allocate();
	this->setLength(length);
	this->setWidth(width);
	this->setHeight(height);
	this->setLengthSegments(lengthSegments);
	this->setWidthSegments(widthSegments);
	this->setHeightSegments(heightSegments);
}
BCube::~BCube()
{
	member_release();
}

void BCube::setLength(float length)
{
	if (member.length != length)
	{
		member.length = length;
		this->dirty();
	}
}
float BCube::length() const
{
	return member.length;
}

void BCube::setWidth(float width)
{
	if (member.width != width)
	{
		member.width = width;
		this->dirty();
	}
}
float BCube::width() const
{
	return member.width;
}

void BCube::setHeight(float height)
{
	if (member.height != height)
	{
		member.height = height;
		this->dirty();
	}
}
float BCube::height() const
{
	return member.height;
}

void BCube::setLengthSegments(int lengthSegments)
{
	if (member.lengthSegments != lengthSegments)
	{
		member.lengthSegments = lengthSegments;
		this->dirty();
	}
}
int BCube::lengthSegments() const
{
	return member.lengthSegments;
}

void BCube::setWidthSegments(int widthSegments)
{
	if (member.widthSegments != widthSegments)
	{
		member.widthSegments = widthSegments;
		this->dirty();
	}
}
int BCube::widthSegments() const
{
	return member.widthSegments;
}

void BCube::setHeightSegments(int heightSegments)
{
	if (member.heightSegments != heightSegments)
	{
		member.heightSegments = heightSegments;
		this->dirty();
	}
}
int BCube::heightSegments() const
{
	return member.heightSegments;
}

BBox BCube::refresh()
{
	BSpace space;
	space.min().set(-member.length * 0.5f, -member.width * 0.5f, 0);
	space.max().set(member.length * 0.5f, member.width * 0.5f, member.height);
	member.rebuild();
	return space;
}
void BCube::render(BRender& render)
{
	BGeometry::render(render);
	if (this->vertices())
	{
		render.setDepthTest(false);
		render.setPointSize(5);
		render.setColor(255, 0, 0);
		BVectorArray* vertices = this->vertices();
		for (int i = 0; i < member.innerIndices.size(); i++)
		{
			int index = member.innerIndices[i];
			render.drawPoint((*vertices)[index]);
		}
	}
}
